import { _decorator, Label, tween, Tween, UITransform } from 'cc';
import { $planet_BasePop_poof$ } from '../../../frame/component/ui.pop';
import $planet_ServiceAPI_poof$ from '../api/service.api';
import $planet_GameAssets_poof$ from '../manager/game-assets';
import type { $planet_Pop_poof$ } from '../ui/pop';
const { ccclass, property } = _decorator;

@ccclass('$planet_Gift_poof$')
export class $planet_Gift_poof$ extends $planet_BasePop_poof$<typeof $planet_Pop_poof$, Awaited<ReturnType<typeof $planet_ServiceAPI_poof$.$planet_giftGet_poof$>>> {
	protected $planet_default_return_poof$: null;

	private $planet_ui_wheel_poof$: UITransform;
	private $planet_ui_wheel_label_poof$: UITransform;

	protected $planet_initAfterOnLoad_poof$(){
		[this.$planet_ui_wheel_poof$, this.$planet_ui_wheel_label_poof$] = ['wheel', 'wheel-label', 'pointer'].map(name=>this.node.getChildByName(name).getComponent(UITransform));
		this.$planet_renderLocalUI_poof$();
	}

	public $planet_onShow_poof$(){
		this.$planet_ui_wheel_label_poof$.node.angle = 0;
		this.$planet_ui_wheel_poof$.node.angle = 0;
		$planet_BasePop_poof$.$planet_jelly_enter_poof$(this.node)
		.then(()=>this.$planet_run_poof$())
		.then(res=>$planet_BasePop_poof$.$planet_jelly_leave_poof$(this.node).then(()=>res))
		.then(res=>this.$planet_close_poof$(res));
	}

	private async $planet_run_poof$(){
		let result = await $planet_ServiceAPI_poof$.$planet_giftGet_poof$();
		await this.$planet_spin_poof$(result.$planet_goodsId_poof$);
		return result;
	}

	private $planet_renderLocalUI_poof$(){
		this.$planet_ui_wheel_poof$.node.children.forEach(n=>abd.$planet_cc_poof$.$planet_skinPeeler_poof$(n, $planet_GameAssets_poof$.$planet_getCounTexture_poof$(0), true));
		(<const>[
			[0, $planet_GameAssets_poof$.$planet_getLangText_poof$(6)],
			[1, $planet_GameAssets_poof$.$planet_getLangText_poof$(8)],
			[2, $planet_GameAssets_poof$.$planet_getLangText_poof$(9)],
			[3, $planet_GameAssets_poof$.$planet_getLangText_poof$(7)],
			[4, $planet_GameAssets_poof$.$planet_getLangText_poof$(8)],
			[5, $planet_GameAssets_poof$.$planet_getLangText_poof$(9)],
		]).forEach(([i, text])=>{
			this.$planet_ui_wheel_label_poof$.node.getChildByName('lbl-'+i).getComponent(Label).string = text;
		});
	}

	private $planet_spin_poof$(id: number){
		let duration = 5;

		let range = id * 60 + abd.$planet_number_poof$.$planet_random_poof$(-25, 25);
		let target = 6 * -360 + range;

		let np = new abd.$planet_NudityPromise_poof$<'end'>();

		[this.$planet_ui_wheel_label_poof$.node, this.$planet_ui_wheel_poof$.node]
		.forEach(n=>{
			Tween.stopAllByTarget(n);

			tween(n).set({angle: 0})
			.to(duration, {angle: target}, {easing: 'expoInOut'})
			.start();
		});

		this.scheduleOnce(()=>np.$planet_resolve_poof$('end'), duration+0.3);

		return np.$planet_promise_poof$;
	}
}

